package com.wwp.tbshop.accout_center.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.wwp.tbshop.R;
import com.wwp.tbshop.accout_center.presenter.Login_presenter;
import com.wwp.tbshop.model.bean.LoginBean;
import com.wwp.tbshop.util.SPUtils;
import com.wwp.tbshop.util.UiUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by 楠GG on 2017/6/15.
 */

public class Login_frament extends Fragment {

    @Bind(R.id.username)
    EditText mUsername;
    @Bind(R.id.password)
    EditText mPassword;
    @Bind(R.id.login)
    Button mLogin;
    private Context mContext;
    public Login_presenter mLogin_presenter;

    public Login_frament(Context context) {
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View inflate = View.inflate(getActivity(), R.layout.login_text, null);
        ButterKnife.bind(this, inflate);
        mLogin_presenter = new Login_presenter(this);
        return inflate;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.login)
    public void onClick() {
//        Toast.makeText(getActivity(), "登录页面", Toast.LENGTH_SHORT).show();

        String username = mUsername.getText().toString().trim();
        String password = mPassword.getText().toString().trim();
        if (TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
            Toast.makeText(getActivity(), "输入信息不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 登录
        mLogin_presenter.LoginUserNewsInfo(username,password);

    }

    public void loginSuccess(LoginBean loginBean) {
        Toast.makeText(UiUtil.sContext,"登录成功", Toast.LENGTH_SHORT).show();

        // 保存密码到本地
        String userid = loginBean.getUserInfo().getUserid();
        SPUtils.putString(UiUtil.sContext,SPUtils.USERID,userid);

        //跳转 界面
        getActivity().finish();
    }
}
